<?php

namespace app\api\controller;

use app\admin\utils\ontimeLog;
use app\api\model\ProductModel;
use app\api\utils\jsonResult;
use think\Controller;


class Product extends Controller
{

    public function _initialize()
    {

    }

    /**********JsonAPI接口***********/
    public function getProductList(): \think\response\Json
    {
        $page = input('param.page');
        $limit = input('param.limit');
        $param = [];
        empty(input('param.keyword')) ?: $param['product.title'] = ['like', '%' . input('param.keyword') . '%'];
        empty(input('param.cid')) ?: $param['product.cid'] = ['=', input('param.cid')];
        $productModel = new ProductModel();
        $dataList = $productModel->list($page, $limit, $param);
        $count = empty($dataList) ? 0 : count($dataList);
        ontimeLog::mylog($count, __FILE__, __METHOD__);

        return (new jsonResult)::result(0, "获取商品列表成功", $dataList, $count);
    }

    public function productDetail(): \think\response\Json
    {
        $pro_no = input('param.pro_no');
        if (empty($pro_no)) {
            return (new jsonResult)::result(400, "缺少参数");
        }
        $productModel = new ProductModel();
        $dataList = $productModel->detail($pro_no);
        if (empty($dataList)) {
            return (new jsonResult)::result(404, "找不到该商品");
        }
//        ontimeLog::mylog($dataList, __FILE__, __METHOD__);
        $dataList = $dataList[0];
        $product_id = $dataList['id'];
        $skuData = $productModel->getSku($product_id);
        $dataList = array_diff_key($dataList, ['id' => '']);
        $dataList['sku_data'] = $skuData;
        return (new jsonResult)::result(200, "获取商品详情成功", $dataList);
    }

    public function productSearch(): \think\response\Json
    {
        $page = input('param.page');
        $limit = input('param.limit');
        $keyword = input('param.keyword');

        if (empty($keyword)) {
            return (new jsonResult)::result(400, "缺少参数");
        }

        $productModel = new ProductModel();
        $dataList = $productModel->search($keyword, $page, $limit);
        $count = $dataList['count'];
        return (new jsonResult)::result(200, "查询成功", $dataList);
    }

}
